package net.uhuijia.goods.goodsku.service.impl;

import net.uhuijia.dao.param.SkuInvertoryParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.uhuijia.appbase.service.CrudService;
import net.uhuijia.goods.goodsku.persistence.dao.GoodSkuDao;
import net.uhuijia.goods.goodsku.persistence.model.GoodSku;
import net.uhuijia.goods.goodsku.persistence.model.GoodSkuExample;
import net.uhuijia.goods.goodsku.service.GoodSkuService;


@Service
@Transactional
public class GoodSkuServiceImpl
     extends CrudService<GoodSkuDao,GoodSku,GoodSkuExample>
     implements  GoodSkuService {
	@Autowired
	private GoodSkuDao goodSkuDao;

	@Autowired
	private GoodSkuService goodSkuService;
	
	@Override
	public GoodSku addGoodSku(String goodId, Double price, String showSkuName, int invertory, String image,
			String groupPropertyId) {
		GoodSku goodSku = new GoodSku();
		this.initDefalutFields(goodSku);
		goodSku.setGoodId(goodId);
		goodSku.setPrice(price);
		goodSku.setShowSkuName(showSkuName);
		goodSku.setInvertory(invertory);
		goodSku.setImage(image);
		goodSkuService.add(goodSku);
		return goodSku;
	}
	/**
	 * @Author xzj
	 * @Description //获取商品库存
	 * @Date  2018/10/8
	 * @Param
	 * @return
	 **/
	@Override
	public int getGoodSkuInvertory(String spuId, String groupPropertyId) {
		Integer count = this.goodSkuDao.getGoodSkuInvertory(new SkuInvertoryParam(spuId,groupPropertyId));
		return count==null?0:count.intValue();
	}

	/**
	 * @Author xzj
	 * @Description // 检查商品规格库存是否足够
	 * @Date  2018/10/8
	 * @Param
	 * @return
	 **/
	@Override
	public Boolean checkGoodSkuInvertoryIsEnough(String spuId, String groupPropertyId, Integer count) {
		if(!StringUtils.isNoneBlank(spuId,groupPropertyId) || count==null ||count.intValue() ==0)
			throw  new IllegalArgumentException();
		int invertory = this.getGoodSkuInvertory(spuId,groupPropertyId);
		if(count<= invertory){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * @Author xzj
	 * @Description //根据spuId和匹配Id查询商品Sku
	 * @Date  2018/10/8
	 * @Param
	 * @return
	 **/
	@Override
	public GoodSku getGoodSkuBySpuIdAndPropertyId(String spuId, String groupPropertyId) {
		if(!StringUtils.isNoneBlank(spuId,groupPropertyId))
			throw new  IllegalArgumentException();
		GoodSku goodSku = this.goodSkuDao.getGoodSkuBySpuIdAndPropertyId(new SkuInvertoryParam(spuId,groupPropertyId));
		return goodSku;
	}
}